﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.Module;

namespace LPV_POS.mh_pos.mh_khac
{
    public partial class mh_congnobandauphaithu : DevExpress.XtraEditors.XtraForm
    {
        Boolean m_themmoi = false;
        Mod_messagebox mess;
        public mh_congnobandauphaithu()
        {
            InitializeComponent();
        }

        private void bton_khachhang_Click(object sender, EventArgs e)
        {
            mh_select_khachhang frm_khachhang = new mh_select_khachhang();
            frm_khachhang.ShowDialog();
            txt_tenkhachhang.Text = frm_khachhang.m_tenkhachhang;
            txt_makhachhang.Text = frm_khachhang.m_makhachhang;
        }

        private void bton_sotien_Click(object sender, EventArgs e)
        {
            string str_sotien = new cpublic().loadformso_point(500, 270).Replace(".","");
            float f_sotien=0;
            if (str_sotien != "")
                float.TryParse(str_sotien, out f_sotien);
            txt_sotien.Text = f_sotien.ToString("N0");
        }

        private void bton_ghichu_Click(object sender, EventArgs e)
        {
            string str_ghichu = new cpublic().loadformchu_point(350, 250);
            if (str_ghichu != "")
                txt_ghichu.Text = str_ghichu;
        }

        private void bton_them_Click(object sender, EventArgs e)
        {
            txt_tenkhachhang.Text = "";
            txt_makhachhang.Text = "";
            txt_sotien.Text = "";
            txt_ghichu.Text = "";
            m_themmoi = true;
        }

        private void bton_sua_Click(object sender, EventArgs e)
        {
            m_themmoi = false;
        }

        private void grid_khachhang_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (grid_congnobandau.RowCount > 0)
            {
                txt_tenkhachhang.Text = grid_congnobandau.GetRowCellDisplayText(grid_congnobandau.FocusedRowHandle, col_tenkhachhang) + "";
                txt_makhachhang.Text = grid_congnobandau.GetRowCellDisplayText(grid_congnobandau.FocusedRowHandle, col_makhachhang) + "";
                txt_sotien.Text = grid_congnobandau.GetRowCellDisplayText(grid_congnobandau.FocusedRowHandle, col_sotien) + "";
                txt_ghichu.Text = grid_congnobandau.GetRowCellDisplayText(grid_congnobandau.FocusedRowHandle, col_ghichu) + "";
            }

        }

        private void mh_congnobandauphaithu_Load(object sender, EventArgs e)
        {
            luoi_congnobandau.DataSource = new thuvien().SP_SelectDataTable("sp_congnobandauphaithu", new string[] { "@flag", "@choose" }, new object[] { 1, 1 });
        }

        private void bton_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bton_xoa_Click(object sender, EventArgs e)
        {
            if (grid_congnobandau.RowCount > 0)
            {
                mess = new Mod_messagebox();
                int chiso = grid_congnobandau.FocusedRowHandle;
                string str_macongno = grid_congnobandau.GetRowCellDisplayText(chiso, col_id) + "";
                
                int ketqua = new thuvien().SP_Update("sp_congnobandauphaithu", new string[] { "@flag", "@choose", "@ID" }, new object[] { 4, 1,str_macongno });
                 if (ketqua >-1)         // them moi
                {
                    mess.messagebox_pos("Thông báo", "Xóa thành công", loaithongbao.canhbao.GetHashCode());
                    DataView tam = (DataView)luoi_congnobandau.Views[0].DataSource;
                    tam.Delete(chiso);
                }
                 else
                     mess.messagebox_pos("Thông báo", "Xóa không thành công", loaithongbao.canhbao.GetHashCode());
            }
        }

        private void bton_luu_Click(object sender, EventArgs e)
        {
            if (txt_tenkhachhang.Text == "" && txt_sotien.Text == "")
                return;
            int ketqua = -1;
            mess = new Mod_messagebox();
            float f_sotien;
            float.TryParse(txt_sotien.Text, out f_sotien);
            if (m_themmoi == true)         // them moi
            {
                ketqua = new thuvien().SP_Update("sp_congnobandauphaithu", 
                                                                        new string[] { "@flag", "@choose", "@MAKHACHHANG", "@SOTIEN", "@GHICHU" }, 
                                                                        new object[] { 2, 1, txt_makhachhang.Text, f_sotien, txt_ghichu.Text });
                if (ketqua > -1)
                {
                    mess.messagebox_pos("Thông báo", "Thêm thành công", loaithongbao.canhbao.GetHashCode());
                    luoi_congnobandau.DataSource = null;
                    luoi_congnobandau.DataSource = new thuvien().SP_SelectDataTable("sp_congnobandauphaithu", 
                                                                                                                                new string[] { "@flag", "@choose" }, 
                                                                                                                                new object[] { 1, 1 });
                }
                else
                    mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
            }
            else                  // chinh sua
            {
                if (grid_congnobandau.RowCount > 0)
                {
                        int chiso = grid_congnobandau.FocusedRowHandle;
                        string str_macongno = grid_congnobandau.GetRowCellDisplayText(chiso, col_id);
                        ketqua = new thuvien().SP_Update("sp_congnobandauphaithu", 
                                                                                new string[] { "@flag", "@choose","@ID", "@MAKHACHHANG", "@SOTIEN", "@GHICHU" }, 
                                                                                new object[] { 3, 1,str_macongno, txt_makhachhang.Text, f_sotien, txt_ghichu.Text });
                        if (ketqua > -1)
                        {
                            mess.messagebox_pos("Thông báo", "Cập nhật thành công", loaithongbao.canhbao.GetHashCode());
                            luoi_congnobandau.DataSource = null;
                            luoi_congnobandau.DataSource = new thuvien().SP_SelectDataTable("sp_congnobandauphaithu", 
                                                                                                                                        new string[] { "@flag", "@choose" }, 
                                                                                                                                        new object[] { 1, 1 });
                            grid_congnobandau.FocusedRowHandle = chiso;
                        }
                        else
                            mess.messagebox_pos("Thông báo", "Cập nhật không thành công", loaithongbao.canhbao.GetHashCode());
                }

               
            }
            
            
        }
    }
}